import requests
import os
#2021-05-05 19:48 by 炸天帮成员
#修改自_6_和_7_
#如果有一天发现不能爬了，那说明bilibili又改资源请求规则了。
#另外由观察可知，请求头也是随着时间而发生变化的，这个数值很有可能是B站目前所有视频的总数相关的数值，所以可能是每天都在变化的。
USER_AGENT='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400'
RANGE='bytes=0-'
NAME='暴走大事件第8季'
REFERER="https://www.bilibili.com/bangumi/play/ep391229?bsource=baidu_aladdin"
REFERER2='https://www.bilibili.com/video/BV1Xh411r7vi'
#注意url是以upos-sz开头的，不然的话无法请求到资源。且status code目前为止为206
#如果无论如何都找不到upos-sz开头的，那么尝试将/upgcxcode前的内容都修改为之前能请求到的upos-sz，大概率能获取到资源。
#注意添加请求源时，如果B站大的爬取规则没有发生变化，则只需要修改字典中的'url'的value值即可。
#以后可能会弄个界面来直接输入请求源，而不是每次都往代码里添加请求源的方式来替换请求源。
#以后也有可能做一个动态跟踪请求源的东西，模拟浏览器发送请求啥的。
#当然也有可能太懒不想弄，毕竟只是爬着玩，就是vanヾ(ﾟ∀ﾟゞ)
#以下请求源截止2021-05-05 22:00 均可用。
bil_info={'0801':
                    {'30032':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/77/46/296694677/296694677_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620223074&gen=playurlv2&os=coso1bv&oi=2062930117&trid=1cd58392d4ab44eea5c90ce8706b2c9du&platform=pc&upsig=ece26be6f4d2ce02b2485baa4458c5df&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                  'url2':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/77/46/296694677/296694677_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620282139&gen=playurlv2&os=coso1bv&oi=1033477054&trid=5d2ae8c2a76141388cc2ac95d6416492u&platform=pc&upsig=979952d88bd43db2b869831cc1cc1a86&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=1&logo=80000000',
                                      'headers':{'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400',
                                                        'Range':RANGE+'42247058',
                                                         'referer':REFERER,
                                                         'connection':'keep-alive'}},
                      '30280':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/77/46/296694677/296694677_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620223074&gen=playurlv2&os=coso1bv&oi=2062930117&trid=1cd58392d4ab44eea5c90ce8706b2c9du&platform=pc&upsig=024a2cd058a510a2608ba631d1f1650c&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                   'url2':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/77/46/296694677/296694677_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620281819&gen=playurlv2&os=vcache&oi=1033477054&trid=b5dd01a89a1544568fb4d3ea00cda637u&platform=pc&upsig=06b95e0711ce368e2a4e1dcf48fb6fa5&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=1374&mid=0&orderid=0,3&agrr=1&logo=80000000',
                                        'headers':{'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400',
                                                        'Range':RANGE+'25855816',
                                                         'referer':REFERER,
                                                         'connection':'keep-alive'}},
                      'name':NAME+'01'},      #0801 END
                '0802':
                    {'30032':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/80/36/303583680/303583680_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620216385&gen=playurlv2&os=kodobv&oi=2062930117&trid=3f1c93c6289b40e2bb3d524374ad5683u&platform=pc&upsig=234fa4d985634e247ed731189694ec84&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=1,3&agrr=0&logo=40000000',
                                  'url2':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/80/36/303583680/303583680_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620225203&gen=playurlv2&os=kodobv&oi=2062930117&trid=983594fba12d4b0e909a0691a17bfda3u&platform=pc&upsig=b574ac26f8cd396e4a0b39b5213a5e43&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=1,3&agrr=0&logo=40000000',
                                 'headers':{'user-agent':USER_AGENT,
                                                'Range':RANGE+'74860850',
                                                'referer':REFERER2,
                                                'connection':'keep-alive'}},
                     '30280':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/80/36/303583680/303583680_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620216385&gen=playurlv2&os=coso1bv&oi=2062930117&trid=3f1c93c6289b40e2bb3d524374ad5683u&platform=pc&upsig=cc1b21b85e60b292aebbf55c87c46cfb&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                  'url2':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/80/36/303583680/303583680_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620225203&gen=playurlv2&os=coso1bv&oi=2062930117&trid=983594fba12d4b0e909a0691a17bfda3u&platform=pc&upsig=973c3e88f801b6acf3728e82458c102b&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=1,3&agrr=0&logo=40000000',
                                  'headers':{'user-agent':USER_AGENT,
                                                 'Range':RANGE+'45129074',
                                                 'referer':REFERER2,
                                                 'connection':'keep-alive'}},
                     'name':NAME+'02'},      #0802 END
                 '0803':
                      {'30032':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/44/57/308915744/308915744_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620217109&gen=playurlv2&os=kodobv&oi=2062930117&trid=fbccbbd85cbb4260a54503be8d821b43u&platform=pc&upsig=ca917ceafd6ac1b49d282cab6b271687&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'url2':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/44/57/308915744/308915744_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620228305&gen=playurlv2&os=vcache&oi=2062930117&trid=04e00abc2f6b48128901d7a0bf0a3b08u&platform=pc&upsig=389388f69385e4761c18584441d0e3a6&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=3705&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'92987023',
                                    'referer':REFERER,
                                    'connection':'keep-alive'}},
                       '30280':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/44/57/308915744/308915744_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620217109&gen=playurlv2&os=kodobv&oi=2062930117&trid=fbccbbd85cbb4260a54503be8d821b43u&platform=pc&upsig=0f73368693a23c7261d7eadd21329f82&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=1,3&agrr=0&logo=40000000',
                                'url2':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/44/57/308915744/308915744_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620228305&gen=playurlv2&os=vcache&oi=2062930117&trid=04e00abc2f6b48128901d7a0bf0a3b08u&platform=pc&upsig=050121d9b9833b8fb616930bcafc689a&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=3703&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                'headers':{'user-agent':USER_AGENT,
                                'Range':RANGE+'62357573',
                                'referer':REFERER,
                                'connection':'keep-alive'}},
                       'name':NAME+'03'},       #0803 END
                  '0804':
                      {'30032':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/71/52/315315271/315315271_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620219345&gen=playurlv2&os=coso1bv&oi=2062930117&trid=8385ed90c51e48f09a0502274ef29587u&platform=pc&upsig=92645b8c8a8c398af461d5ff03e05a32&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                'url2':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/71/52/315315271/315315271_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620228645&gen=playurlv2&os=vcache&oi=2062930117&trid=efea4619f33e4941b361b71d4aaf2dcau&platform=pc&upsig=4197f8f1dcd1d1cf18b23ebba74f2de2&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6414&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                'headers':{'user-agent':USER_AGENT,
                                'Range':RANGE+'105458956',
                                'referer':REFERER2,
                                'connection':'keep-alive'}},
                       '30280':{'url':'https://upos-sz-mirrorkodoo1.bilivideo.com/upgcxcode/71/52/315315271/315315271_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620219345&gen=playurlv2&os=kodoo1bv&oi=2062930117&trid=8385ed90c51e48f09a0502274ef29587u&platform=pc&upsig=5d5211f43e73b4f4577c322545944159&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                'url2':'https://upos-sz-mirrorkodoo1.bilivideo.com/upgcxcode/71/52/315315271/315315271_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620228645&gen=playurlv2&os=vcache&oi=2062930117&trid=efea4619f33e4941b361b71d4aaf2dcau&platform=pc&upsig=31a2479e7276aae5c63abc2dfee3e591&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=1374&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                'headers':{'user-agent':USER_AGENT,
                                'Range':RANGE+'60210381',
                                'referer':REFERER2,
                                'connection':'keep-alive'}},
                       'name':NAME+'04'},       #0804 END
                  '0805':
                      {'30032':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/67/34/321493467/321493467_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620220640&gen=playurlv2&os=vcache&oi=2062930117&trid=f75a19b22fb04ca681f126ff95efd0a8u&platform=pc&upsig=a0a159df45d3097d257199d1ed3b20bb&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=3711&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'url2':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/67/34/321493467/321493467_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620228882&gen=playurlv2&os=vcache&oi=2062930117&trid=955ec7e4d80848b790c503d9b991d91bu&platform=pc&upsig=2d6057ec1fd5ad392da4fb007711df00&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6591&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'94129921',
                                    'referer':REFERER2,
                                    'connection':'keep-alive'}},
                       '30280':{'url':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/67/34/321493467/321493467_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620220829&gen=playurlv2&os=vcache&oi=2062930117&trid=3b4c2df4a0ff4372a055dd5bcfdca862u&platform=pc&upsig=fb085ac302a2bb02e48ea9c327f9b8c8&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6594&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'url2':'https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/67/34/321493467/321493467_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620228882&gen=playurlv2&os=vcache&oi=2062930117&trid=955ec7e4d80848b790c503d9b991d91bu&platform=pc&upsig=003c50379afe5c181e10b71de3efba8b&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6594&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'57892497',
                                    'referer':REFERER2,
                                    'connection':'keep-alive'}},
                       'name':NAME+'05'},       #0805 END
                  '0806':
                      {'30032':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/41/44/327594441/327594441_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620222102&gen=playurlv2&os=kodobv&oi=2062930117&trid=d6654f2ce8bc473290f1be696a4ba274u&platform=pc&upsig=15daa1cf7d7ef9f92d85c8e22162a779&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'url2':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/41/44/327594441/327594441_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620229950&gen=playurlv2&os=vcache&oi=2062930117&trid=374043834a654833b8cb84304184971eu&platform=pc&upsig=55ddea6e386d3dd03599052ac83f5912&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6593&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'73890133',
                                    'referer':REFERER,
                                    'connection':'keep-alive'}},
                       '30280':{'url':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/41/44/327594441/327594441_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620222102&gen=playurlv2&os=kodobv&oi=2062930117&trid=d6654f2ce8bc473290f1be696a4ba274u&platform=pc&upsig=da1bb8fb58b06cb7e53e1eb5bff6d439&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'url2':'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/41/44/327594441/327594441_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620229950&gen=playurlv2&os=vcache&oi=2062930117&trid=374043834a654833b8cb84304184971eu&platform=pc&upsig=b0b28ae2842a1da2750e2965c5a04797&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6588&mid=0&orderid=0,3&agrr=0&logo=80000000',
                                    'headers':{'user-agent':USER_AGENT,
                                    'Range':RANGE+'40968938',
                                    'referer':REFERER,
                                    'connection':'keep-alive'}},
                       'name':NAME+'06'},       #0806 END
              }
#--------------------------------------------------------------------------------------------
mainFileDir=f'.\\pyBilibili\\'
if os.path.exists(mainFileDir):
    pass
else:
    os.mkdir(mainFileDir)
#--------------------------------------------------------------------------------------------
MOV_INDEX=801
MOV_INDEX_MAX=800+len(bil_info)  #以后修改此处即可替换最大集数
def getSource(Https,Headers,Address):
        '''
        函数功能：用于获取请求到的网络资源，并保存到本地。
        传入参数：Https 即 Request URL
                        Headers 即 Response Headers & Request Headers
                        Address 即 最终文件保存所在地
        返回参数：fileContent 即 从网络获取到的资源的2进制数据，将在changeUrl(FileContent,FileTypeTag,Address)函数中被用于判断文件大小。
        '''
        r=requests.get(Https,headers=Headers)
        f=open(Address,'wb')
        f.write(r.content)
        f.close()
        f=open(Address,"rb")
        fileContent=f.read()
        f.close()
        return fileContent

def changeUrl(FileContent,FileTypeTag,Address):
    '''
    函数功能：判断保存至本地的文件的大小，并返回是否下载成功标志位。
    传入参数：FileContent 即 2进制文件的数据
                    FileTypeTag 即 用于切换显示文字 的判断用
                    Address 即 被判断的本地文件的所在文件地址。
    返回参数：
                    一个字符串。当文件过小时，返回'下载失败'，以便后续再次使用getSource(Https,Headers,Address)进行请求源的切换；否则就是下载成功。
    '''
    if FileTypeTag == 'video':
        text='视频部分'
    elif FileTypeTag == 'voice':
        text='音频部分'
    if len(FileContent)<40960:
        print(Address+'疑似无效文件！')
        os.remove(Address)
        print(Address+'已删除！')
        return 'DLD False!'
    else:
        print(Address+text+'已经下载完成')
        return 'DLD Success!'
    

while MOV_INDEX<=MOV_INDEX_MAX :
    videoFile_m4s=bil_info['0'+str(MOV_INDEX)]['name']+'_video'+'.mp4'
    if os.path.exists(mainFileDir+videoFile_m4s):
        print('已存在'+videoFile_m4s)
        pass
    else:
##        r=requests.get(bil_info['0'+str(MOV_INDEX)]['30032']['url'],headers=bil_info['0'+str(MOV_INDEX)]['30032']['headers'])
##        f=open(mainFileDir+videoFile_m4s,'wb')
##        f.write(r.content)
##        f.close()
##        f=open(mainFileDir+videoFile_m4s,"rb")
##        videoFile=f.read()
##        f.close()
### 上方注释将被下列语句替换。
        videoFile=getSource(bil_info['0'+str(MOV_INDEX)]['30032']['url'],bil_info['0'+str(MOV_INDEX)]['30032']['headers'],mainFileDir+videoFile_m4s)
##        if len(videoFile)<40960:
##            print(videoFile_m4s+'疑似无效文件！')
##            os.remove(mainFileDir+videoFile_m4s)
##            print(videoFile_m4s+'已删除！')
##        else:
##            print(videoFile_m4s+'视频部分已经下载完成')
### 上方注释将被下列语句替换。
        DLD_Flag=changeUrl(videoFile,'video',mainFileDir+videoFile_m4s)
        if DLD_Flag=='DLD False!':
            print("尝试更换请求源...")
            try:
                videoFile=getSource(bil_info['0'+str(MOV_INDEX)]['30032']['url2'],bil_info['0'+str(MOV_INDEX)]['30032']['headers'],mainFileDir+videoFile_m4s)
                DLD_Flag=changeUrl(videoFile,'video',mainFileDir+videoFile_m4s)
                if DLD_Flag=='DLD False!':
                    print("尝试失败，请新增请求源!")
                else:
                    print("更换成功！")
            except KeyError:#因为bil_info字典里的条目有些没有设置'url2'替补源，以后添加'urlxx'时也是一样，到时候此处的try需要修改一下
                print(videoFile_m4s+'不存在替补源，请新增请求源!')
                pass

    voiceFile_m4s=bil_info['0'+str(MOV_INDEX)]['name']+'_voice'+'.mp4'
    if os.path.exists(mainFileDir+voiceFile_m4s):
        print('已存在'+voiceFile_m4s)
        pass
    else:
##        r=requests.get(bil_info['0'+str(MOV_INDEX)]['30280']['url'],headers=bil_info['0'+str(MOV_INDEX)]['30280']['headers'])
##        f=open(mainFileDir+voiceFile_m4s,'wb')
##        f.write(r.content)
##        f.close()
##        f=open(mainFileDir+voiceFile_m4s,"rb")
##        voiceFile=f.read()
##        f.close()
##        if len(voiceFile)<40960:
##            print(voiceFile_m4s+'疑似无效文件！')
##            os.remove(mainFileDir+voiceFile_m4s)
##            print(voiceFile_m4s+'已删除！')
##        else:
##            print(voiceFile_m4s+'音频部分已经下载完成')
        voiceFile=getSource(bil_info['0'+str(MOV_INDEX)]['30280']['url'],bil_info['0'+str(MOV_INDEX)]['30280']['headers'],mainFileDir+voiceFile_m4s)
        DLD_Flag=changeUrl(voiceFile,'voice',mainFileDir+voiceFile_m4s)
        if DLD_Flag=='DLD False!':
            print("尝试更换请求源...")
            try:
                voiceFile=getSource(bil_info['0'+str(MOV_INDEX)]['30280']['url2'],bil_info['0'+str(MOV_INDEX)]['30280']['headers'],mainFileDir+voiceFile_m4s)
                DLD_Flag=changeUrl(voiceFile,'voice',mainFileDir+voiceFile_m4s)
                if DLD_Flag=='DLD False!':
                    print("尝试失败，请新增请求源!")
                else :
                    print("更换成功!")
            except KeyError:#因为bil_info字典里的条目有些没有设置'url2'替补源，以后添加'urlxx'时也是一样，到时候此处的try需要修改一下
                print(voiceFile_m4s+'不存在替补源，请新增请求源!')
                pass
    MOV_INDEX+=1

#print(len(bil_info))
###---------------------------------------------------------------------------------------------
##r=requests.get(bil_info['0802']['30032']['url'],headers=bil_info['0802']['30032']['headers'])
##videoFile_m4s=bil_info['0802']['name']+'_video'+'.mp4'
##f=open(videoFile_m4s,'wb')
##f.write(r.content)
##f.close()
##print(videoFile_m4s+'视频部分已经下载完成')
##
##r=requests.get(bil_info['0802']['30280']['url'],headers=bil_info['0802']['30280']['headers'])
##voiceFile_m4s=bil_info['0802']['name']+'_voice'+'.mp4'
##f=open(voiceFile_m4s,'wb')
##f.write(r.content)
##f.close()
##print(voiceFile_m4s+'音频部分已经下载完成')
###--------------------------------------------------------------------------------------------
##r=requests.get(bil_info['0803']['30032']['url'],headers=bil_info['0803']['30032']['headers'])
##videoFile_m4s=bil_info['0803']['name']+'_video'+'.mp4'
##f=open(videoFile_m4s,'wb')
##f.write(r.content)
##f.close()
##print(videoFile_m4s+'视频部分已经下载完成')
##
##r=requests.get(bil_info['0803']['30280']['url'],headers=bil_info['0803']['30280']['headers'])
##voiceFile_m4s=bil_info['0803']['name']+'_voice'+'.mp4'
##f=open(voiceFile_m4s,'wb')
##f.write(r.content)
##f.close()
##print(voiceFile_m4s+'音频部分已经下载完成')
###--------------------------------------------------------------------------------------------
